package com.umeox.auto_light

import android.os.Bundle
import android.view.View
import android.view.WindowManager
import android.widget.Button
import android.widget.ImageView
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView

const val TAG = "AutoLight"

class AutoLightActivity : AppCompatActivity(),
    BaseRecyclerViewAdapter.OnItemClickListener<DeviceProxy> {

    private var adapter = DeviceListAdapter()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_auto_light)

        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)

        findViewById<Button>(R.id.bt_start).setOnClickListener {
            findViewById<Button>(R.id.bt_start).visibility = View.GONE
            DeviceListManager.startTask()
        }

        findViewById<ImageView>(R.id.iv_help).setOnClickListener {
            AlertDialog.Builder(this)
                .setTitle("信息")
                .setMessage("自动全亮屏工具，如不可用，请关闭app重新开始")
                .setNegativeButton(
                    "确定"
                ) { dialog, _ -> dialog.dismiss() }
                .create()
                .show()
        }

        val deviceRv = findViewById<RecyclerView>(R.id.rv_device_list)
        deviceRv.adapter = adapter
        adapter.setOnItemClickListener(this)

        DeviceListManager.devicesObservable.observe(this) {
            adapter.refreshData(it)
            deviceRv.scrollToPosition(adapter.itemCount -1)
        }
    }

    override fun onItemClick(view: View?, position: Int, t: DeviceProxy) {
        AlertDialog.Builder(this)
            .setTitle("信息")
            .setMessage("当前设备 ${t.getDeviceInfo()}")
            .setNegativeButton(
                "确定"
            ) { dialog, _ -> dialog.dismiss() }
            .create()
            .show()
    }

    override fun onBackPressed() {}
}